<template>
    <div class="login-bg">
        <div class="login-container">
            <div class="login-header">
                <img class="logo mr10" src="../../assets/img/logo.svg" alt="">
                <div class="login-title">
                    宠物领养后台管理系统
                </div>
            </div>
            <el-form :model="param" :rules="rules" ref="register" size="large">
                <el-form-item prop="admin_name">
                    <el-input v-model="param.admin_name" placeholder="用户名">
                        <template #prepend>
                            <el-icon>
                                <User />
                            </el-icon>
                        </template>
                    </el-input>
                </el-form-item>
                <el-form-item prop="admin_email">
                    <el-input v-model="param.admin_email" placeholder="邮箱">
                        <template #prepend>
                            <el-icon>
                                <Message />
                            </el-icon>
                        </template>
                    </el-input>
                </el-form-item>

                <el-form-item prop="admin_phone">
                    <el-input v-model="param.admin_phone" placeholder="手机">
                        <template #prepend>
                            <el-icon>
                                <Phone />
                            </el-icon>
                        </template>
                    </el-input>
                </el-form-item>


                <el-form-item prop="admin_pwd">
                    <el-input type="password" placeholder="密码" v-model="param.admin_pwd"
                        @keyup.enter="submitForm(register)">
                        <template #prepend>
                            <el-icon>
                                <Lock />
                            </el-icon>
                        </template>
                    </el-input>
                </el-form-item>
                <el-button class="login-btn" type="primary" size="large" @click="submitForm(register)">注册</el-button>
                <p class="login-text">已有账号，<el-link type="primary" @click="$router.push('/login')">立即登录</el-link></p>
            </el-form>
        </div>
    </div>
</template>

<script setup lang="ts">

import { ref, reactive } from 'vue';

import { useRouter } from 'vue-router';

import { ElMessage, type FormInstance, type FormRules } from 'element-plus';

import { registerAPI } from '../../api/index';

// TS规范表单信息对象
interface Admin {
    admin_name: string;
    admin_email: string;
    admin_phone: string;
    admin_pwd: string;
}

// 创建路由
const router = useRouter();

// 表单的字段
const param = reactive<Admin>({
    admin_name: '',
    admin_email: '',
    admin_pwd: '',
    admin_phone: ''
});

// 正则表达式（做用户反馈）
const rules: FormRules = {
    admin_name: [
        {
            required: true,
            message: '请输入用户名',
            trigger: 'blur',
        },
    ],
    admin_pwd: [{ required: true, message: '请输入密码', trigger: 'blur' }],
    admin_email: [{ required: true, message: '请输入邮箱', trigger: 'blur' }],
};

const register = ref<FormInstance>();

const submitForm = (formEl: FormInstance | undefined) => {

    // 检查param对象
    // console.log(param)

    // 如果表单输入框为空，就终止代码
    if (!formEl) return;

    // 否则，有值
    formEl.validate( async (valid: boolean) => {
        if (valid) {
            // 解构赋值，获取表单信息
            let { admin_name,admin_email,admin_phone,admin_pwd } = param;
            
            // 调用注册接口
            await registerAPI({admin_name,admin_email,admin_phone,admin_pwd})
            .then(
                content=>{
                    console.log("成功",content)
                    let {code , msg } = content;
                    if(code == 200) {
                        ElMessage.success(msg);
                        setTimeout(()=>{
                              router.push('/login');
                        },1000)
                    }
                    else {
                        ElMessage.error(msg);
                    }
                },
                error=>{
                    console.log("失败",error)
                    ElMessage.error("请求错误，请检查接口参数、URL");
                }
            )
        } else {
            return false;
        }
    });
};

</script>

<style scoped>
.login-bg {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 100%;
    height: 100%;
    background: url(../../assets/img/login-bg.jpg) center/cover no-repeat;
}

.login-header {
    display: flex;
    align-items: center;
    justify-content: center;
    margin-bottom: 40px;
}

.logo {
    width: 35px;
}

.login-title {
    font-size: 22px;
    color: #333;
    font-weight: bold;
}

.login-container {
    width: 450px;
    border-radius: 5px;
    background: #fff;
    padding: 40px 50px 50px;
    box-sizing: border-box;
}

.login-btn {
    display: block;
    width: 100%;
}

.login-text {
    display: flex;
    align-items: center;
    margin-top: 20px;
    font-size: 14px;
    color: #787878;
}
</style>
